package com.snc.controller.hxsq.resident;


import com.snc.annotation.SystemLog;
import com.snc.controller.index.BaseController;
import com.snc.entity.PkFormMap;
import com.snc.entity.ResUserFormMap;
import com.snc.entity.ResidentFormMap;
import com.snc.exception.SystemException;
import com.snc.plugin.PageView;
import com.snc.plugin.Status;
import com.snc.util.Common;
import com.snc.util.PasswordHelper;
import com.snc.util.PropertiesUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/resident/")
public class ResidentAdminController extends BaseController {

    @RequestMapping("listUI")
    public String listUI(Model model) throws Exception {
        model.addAttribute("res", findByRes());
        return Common.BACKGROUND_PATH + "/hxsq/resident/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow,
                               String pageSize) throws Exception {
        ResidentFormMap residentFormMap = getFormMap(ResidentFormMap.class);
        residentFormMap.put("isMaster", "N");
        residentFormMap = toFormMap(residentFormMap, pageNow, pageSize);
        if(residentFormMap.getStr("userName") != null && !"".equals(residentFormMap.getStr("userName"))){
            residentFormMap.put("userName", residentFormMap.getStr("userName"));
        }

        pageView.setRecords(ResidentFormMap.mapper().findUserPage(residentFormMap));
        return pageView;
    }


    @RequestMapping("listUI2")
    public String listUI2(Model model) throws Exception {
        model.addAttribute("res", findByRes());
        return Common.BACKGROUND_PATH + "/hxsq/resident/list2";
    }

    @ResponseBody
    @RequestMapping("findByPage2")
    public PageView findByPage2(String pageNow,
                                String pageSize) throws Exception {
        ResidentFormMap residentFormMap = getFormMap(ResidentFormMap.class);
        residentFormMap.put("isMaster", "Y");
        residentFormMap = toFormMap(residentFormMap, pageNow, pageSize);
        if(residentFormMap.getStr("userName") != null && !"".equals(residentFormMap.getStr("userName"))){
            residentFormMap.put("userName", residentFormMap.getStr("userName"));
        }

        pageView.setRecords(ResidentFormMap.mapper().findUserPage(residentFormMap));
        return pageView;
    }

    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return Common.BACKGROUND_PATH + "/hxsq/resident/add";
    }

    @ResponseBody
    @RequestMapping("addEntity")
    @SystemLog(module = "信息管理", methods = "政策法规管理-新增政策法规")
    @Transactional(readOnly = false)
    public String addEntity() {
        try {

            ResidentFormMap residentFormMap = super.getFormMap(ResidentFormMap.class);
            //查询该手机号码是否已注册
            List<ResUserFormMap> results = new ResidentFormMap().findByAttribute("phone", residentFormMap.get("phone"));
            if (results != null && results.size() > 0) {
                return "该手机号码已存在";
            }

            residentFormMap.put("createTime", new Date());
            residentFormMap.put("status", Status.Y);
            PasswordHelper passwordHelper = new PasswordHelper();
            passwordHelper.encryptPassword(residentFormMap);

            residentFormMap.save();

        } catch (Exception e) {
            //try catch 必须抛出自定义异常,否则事务失效!   #### lanyuan ####
            String results = "{\"results\":\"error\",\"message\":\"" + e + "\"}";
            throw new SystemException(results);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional(readOnly = false)
    @SystemLog(module = "信息管理", methods = "政策法规管理-删除政策法规")
    public String deleteEntity(String ids) throws Exception {
        for (String id : ids.split(",")) {
            ResidentFormMap map = new ResidentFormMap();
            map.put("deleted", Status.Y.toString());
            map.put("id", id);
            map.update();
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("auth")
    @Transactional(readOnly = false)
    public String auth(String ids) throws Exception {
        for (String id : ids.split(",")) {
            ResidentFormMap map = new ResidentFormMap();
            map.put("isAuth", Status.Y.toString());
            map.put("id", id);
            map.update();
        }
        return "success";
    }
    
    @ResponseBody
    @RequestMapping("notauth")
    @Transactional(readOnly = false)
    public String notAuth(String ids) throws Exception {
        for (String id : ids.split(",")) {
            ResidentFormMap map = new ResidentFormMap();
            map.put("isAuth", Status.N.toString());
            map.put("id", id);
            map.update();
        }
        return "success";
    }

    @RequestMapping("editUI")
    public String editUI(Model model) throws Exception {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            String imgHost = PropertiesUtils.getProperties().getProperty("imgHost");
            model.addAttribute("imgHost", imgHost);
            model.addAttribute("resident", new ResidentFormMap().findById(id));
        }
        return Common.BACKGROUND_PATH + "/hxsq/resident/edit";
    }

    @RequestMapping("handselUI")
    public String handselUI(Model model) throws Exception {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            model.addAttribute("resident", new ResidentFormMap().findById(id));
        }
        return Common.BACKGROUND_PATH + "/hxsq/resident/handsel";
    }

    @RequestMapping("info")
    public String info(Model model) throws Exception {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            model.addAttribute("jx", new ResidentFormMap().findById(id));
        }
        return Common.BACKGROUND_PATH + "/hxsq/resident/info";
    }

    @RequestMapping("course")
    public String course(Model model) throws Exception {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            model.addAttribute("jx", new ResidentFormMap().findById(id));
            PkFormMap map = new PkFormMap();
            
           
            model.addAttribute("courses", map.findByAttribute("teacherId", id));
        }
        return Common.BACKGROUND_PATH + "/hxsq/resident/course";
    }

    @ResponseBody
    @RequestMapping("editEntity")
    @Transactional(readOnly = false)
    @SystemLog(module = "系统管理", methods = "用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity(HttpServletRequest request) throws Exception {
        try {
            ResidentFormMap residentFormMap = new ResidentFormMap();

            String type = request.getParameter("type");

            residentFormMap.put("id", request.getParameter("id"));
            residentFormMap.put("title", request.getParameter("title"));
            residentFormMap.put("type", type);
            residentFormMap.put("status", request.getParameter("status"));
            residentFormMap.put("sort", request.getParameter("sort"));

        } catch (Exception e) {
            //try catch 必须抛出自定义异常,否则事务失效!   #### lanyuan ####
            String results = "{\"results\":\"error\",\"message\":\"" + e + "\"}";
            throw new SystemException(results);
        }
        return "success";
    }

    @RequestMapping("updateState")
    @ResponseBody
    @Transactional
    public String updateState() throws Exception {
        ResidentFormMap residentFormMap = getFormMap(ResidentFormMap.class);
        residentFormMap.update();
        return "success";
    }

    @RequestMapping("updateMaster")
    @ResponseBody
    @Transactional
    public String updateMaster() throws Exception {
        ResidentFormMap residentFormMap = getFormMap(ResidentFormMap.class);
        residentFormMap.update();
        return "success";
    }

}